/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.goods.service.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.medusa.gruul.common.core.exception.ServiceException;
import com.medusa.gruul.common.core.util.SystemCode;
import com.medusa.gruul.common.data.tenant.ShopContextHolder;
import com.medusa.gruul.common.data.tenant.TenantContextHolder;
import com.medusa.gruul.goods.api.entity.RebateSet;
import com.medusa.gruul.goods.api.model.dto.manager.RebateSetDto;
import com.medusa.gruul.goods.mapper.manager.RebateSetMapper;
import com.medusa.gruul.goods.service.manager.IRebateSetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 店铺返利信息设置 服务实现类
 * </p>
 *
 * @author lcysike
 * @since 2020-05-21
 */
@Service
public class RebateSetServiceImpl extends ServiceImpl<RebateSetMapper, RebateSet> implements IRebateSetService {

    @Autowired
    private RebateSetMapper rebateSetMapper;

    /**
     * 获取店铺返利信息设置
     *
     * @return RebateSetVo
     */
    @Override
    public RebateSet selectRebateSet() {
        String shopId = ShopContextHolder.getShopId();
        String tenantId = TenantContextHolder.getTenantId();
        return rebateSetMapper.selectOne(new QueryWrapper<RebateSet>().eq("tenant_id", tenantId).eq("shop_id", shopId));
    }

    /**
     * feign获取店铺返利信息设置
     * @param tenantId
     * @param shopId
     * @return RebateSetVo
     */
    @Override
    public RebateSet getRebateSet(String tenantId, String shopId) {
        return rebateSetMapper.selectOne(new QueryWrapper<RebateSet>().eq("tenant_id", tenantId).eq("shop_id", shopId));
    }

    /**
     * 新增/修改店铺返利信息设置
     *
     * @param rebateSetDto
     */
    @Override
    public void saveRebateSet(RebateSetDto rebateSetDto) {
        if(rebateSetDto.getId() != null){
            RebateSet rebateSet = rebateSetDto.coverRebateSet();
            int update = rebateSetMapper.updateById(rebateSet);
            if(update == 0){
                throw new ServiceException("店铺信息设置失败！", SystemCode.DATA_UPDATE_FAILED.getCode());
            }
        }else{
            RebateSet rebateSet = rebateSetDto.coverRebateSet();
            int insert = rebateSetMapper.insert(rebateSet);
            if(insert == 0){
                throw new ServiceException("店铺信息设置失败！", SystemCode.DATA_ADD_FAILED.getCode());
            }
        }
    }
}
